<?php

namespace falcon5\inc\Handlers;

use Exception;
use falcon5\inc\Base\Common;
use falcon5\inc\Base\HandlerInterface;
use falcon5\inc\Infrastructure\Services\MigrateService;

/**
 * Class Activate
 * @package falcon5\inc\Handlers
 * @desc Activation handler
 */
class Activate extends Common implements HandlerInterface
{
    /**
     * Handle plugin activation
     */
    public function handler()
    {
        try {
            error_log('Falcon5: Starting plugin activation');
            
            // Use migration service to execute database migration
            // Only migrate the current site on activation for better performance
            $migrateService = new MigrateService();
            
            // In multisite environment, only migrate the current site during activation
            // This is more efficient than migrating all sites at once
            $result = $migrateService->migrate();
            
            if ($result) {
                error_log('Falcon5: Database migration successful during plugin activation');
            } else {
                error_log('Falcon5: Error occurred during database migration');
            }

            // Store a transient to trigger redirect after activation
            set_transient('falcon5_activation_redirect', true, 30);

        } catch (Exception $e) {
            // Log error
            error_log('Falcon5 Activation error: ' . $e->getMessage());
            error_log('Falcon5 Activation error trace: ' . $e->getTraceAsString());
        }
    }
}